﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CPPEI.WebApi
{
    public static class ActionEntry
    {
        static JsonSerializerSettings s_settings;

        static ActionEntry()
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            s_settings = new JsonSerializerSettings();
            s_settings.Converters.Add(timeFormat);
            if(Config.Current.IsCamelCasePropertyName)
            {
                s_settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }

            CPPEI.WebApi.ActionFactory.Init();
        }

        public static void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json; charset=utf-8";

            ActionResult result = null;
            SysParams sysParams = GetParameter(context.Request);

            if(!string.IsNullOrEmpty(sysParams.Language))
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(sysParams.Language);
            }

            if (string.IsNullOrEmpty(sysParams.Module) || 
                string.IsNullOrEmpty(sysParams.Scenario) || 
                string.IsNullOrEmpty(sysParams.Action))
            {
                result = new ActionResult(false, ErrorCode.Common_InvalidParameter, ErrorCode.ToString(ErrorCode.Common_InvalidParameter), null);
            }
            else
            {
                IAction action = ActionFactory.GetAction(sysParams);
                if (action != null)
                {
                    result = action.Execute(context.Request, sysParams);
                }
                else
                {
                    result = new ActionResult(false, ErrorCode.ActionNotFound, ErrorCode.ToString(ErrorCode.ActionNotFound), null);
                }
            }
            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result, Formatting.Indented, s_settings));
        }

        private static SysParams GetParameter(HttpRequest request)
        {
            SysParams parameters = new SysParams();
            parameters.Module = request.QueryString["sys:mod"];
            parameters.Scenario = request.QueryString["sys:sno"];
            parameters.Action = request.QueryString["sys:act"];
            parameters.Version = request.QueryString["sys:ver"];
            parameters.Token = request.QueryString["sys:token"];
            parameters.Language = request.QueryString["sys:lang"];

            string[] contentTypes = request.ContentType.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (contentTypes.Length > 0)
            {
                if (string.Equals(contentTypes[0], "application/json", StringComparison.CurrentCultureIgnoreCase))
                {
                    parameters.DataType = Enctype.Application_Json;
                }
                else if(string.Equals(contentTypes[0], "application/json", StringComparison.CurrentCultureIgnoreCase))
                {
                    parameters.DataType = Enctype.Multipart_Form_Data;
                }
                else
                {
                    parameters.DataType = Enctype.Application_Form;
                }
            }
            else
            {
                parameters.DataType = Enctype.Application_Form;
            }
            return parameters;
        }

        /*
        private static SysParameters GetParameter(HttpRequest request)
        {
            SysParameters parameters = new SysParameters();
            if(request.HttpMethod == HttpMethods.POST)
            {
                parameters.Scenario = request.Form["sys:sno"];
                parameters.Action = request.Form["sys:act"];
                parameters.Version = request.Form["sys:ver"];
                parameters.Token = request.Form["sys:token"];
            }
            else if(request.HttpMethod == HttpMethods.GET)
            {
                parameters.Scenario = request.QueryString["sys:sno"];
                parameters.Action = request.QueryString["sys:act"];
                parameters.Version = request.QueryString["sys:ver"];
                parameters.Token = request.QueryString["sys:token"];
            }

            return parameters;
        }*/
    }
}
